package com.kj.tms.system.service.org.serviceImpl;

import com.kj.tms.common.bean.dao.BaseMapper;
import com.kj.tms.common.bean.redis.RedisAccess;
import com.kj.tms.common.bean.service.BaseService;
import com.kj.tms.common.utils.StringUtil;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.org.dto.AddressCityContryDto;
import com.kj.tms.pri.system.org.service.AddressCityContryService;
import com.kj.tms.system.service.org.dao.AddressCityContryMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangguoqing
 * @Description: 省市区 redis查询业务类实现类
 * @Date: Created in 10:06 2017/11/1
 * @Company: 北京宏图天安
 * @Version: 2017/11/1
 * @Modified By:
 */

@RestController
public class AddressCityContryServiceImpl extends BaseService<AddressCityContryDto> implements AddressCityContryService {

    private static final Logger logger = LoggerFactory.getLogger(AddressCityContryServiceImpl.class);

    @Autowired
    private AddressCityContryMapper addressCityContryMapper;

    @Autowired
    private RedisAccess redisAccessImpl;

    /**
     * @Title: queryAllSonByCondition
     * @Description 根据父级编码查询全部子级
     * @Method queryAllSonByCondition
     * @Date: 2017年11月01日 10:22
     * @Author:wangguoqing
     * @version: 2017年11月01日
     */
    @Override
    public List<AddressCityContryDto> queryCityAllByCondition() {
        return addressCityContryMapper.queryCityAllByCondition();
    }

    /**
     * @param addressCityContryDto
     * @Title: queryAllAreaByCondition
     * @Description 条件查询全部
     * @Method queryAllAreaByCondition
     * @Date: 2017年11月01日 10:22
     * @Author:wangguoqing
     * @version: 2017年11月01日
     */
    @Override
    public List<AddressCityContryDto> queryAllAreaByCondition(@RequestBody AddressCityContryDto addressCityContryDto) {
        return addressCityContryMapper.queryAllAreaByCondition(addressCityContryDto);
    }

    @Override
    public BaseMapper<AddressCityContryDto> getMapper() {
        return this.addressCityContryMapper;
    }

    /**
     * @param pidCode
     * @Title: listByPidCode
     * @Description 获取公共省地市
     * @Method listByPidCode
     * @Return
     * @Date: 2017年11月01日 10:25
     * @Author:wangguoqing
     * @version: 2017年11月01日
     */
    @Override
    public Map<String, Object> listByPidCode(@RequestParam("pidCode") String pidCode) {
        Map<String, Object> returnObject = new HashMap<>();
        if (pidCode.equals(Consts.ROOT_NODE_PR0V)) {
            if(!StringUtil.isEmptyOrNull(listProvAndCityAndContryFromRedis(Consts.PRVO, pidCode))){
                returnObject.put("rows", listProvAndCityAndContryFromRedis(Consts.PRVO, pidCode));
            }else{//查库
                AddressCityContryDto areaDto =new AddressCityContryDto();
                areaDto.setPidCode("000000");
                List<AddressCityContryDto> provinceList =queryAllAreaByCondition(areaDto);
                returnObject.put("rows", provinceList);
            }
        } else {
            //截取地市为后四位
            String id = pidCode.substring(2);
            String pid = pidCode;
            if (id.equals(Consts.ROOT_NODE_CITY)) {
                if(!StringUtil.isEmptyOrNull(listProvAndCityAndContryFromRedis(Consts.CITY, pid))){
                    returnObject.put("rows", listProvAndCityAndContryFromRedis(Consts.CITY, pid));
                }else{//查库
                    AddressCityContryDto areaDto =new AddressCityContryDto();
                    areaDto.setPidCode(pid);
                    List<AddressCityContryDto> provinceList =queryAllAreaByCondition(areaDto);
                    returnObject.put("rows", provinceList);
                }
            } else {
                if(!StringUtil.isEmptyOrNull(listProvAndCityAndContryFromRedis(Consts.CONTRY, pid))){
                    returnObject.put("rows", listProvAndCityAndContryFromRedis(Consts.CONTRY, pid));
                }else{//查库
                    AddressCityContryDto areaDto =new AddressCityContryDto();
                    areaDto.setPidCode(pid);
                    List<AddressCityContryDto> provinceList =queryAllAreaByCondition(areaDto);
                    returnObject.put("rows", provinceList);
                }
            }
        }
        return returnObject;
    }

    /**
     * @param addressType，addressCode
     * @Title: listByPidCode
     * @Description 获取公共省地市byRedis
     * @Method listProvAndCityAndContryFromRedis
     * @Return
     * @Date: 2017年11月01日 10:25
     * @Author:wangguoqing
     * @version: 2017年11月01日
     */
    private List<AddressCityContryDto> listProvAndCityAndContryFromRedis(String addressType, String addressCode) {
        try {
            List<AddressCityContryDto> addressList = redisAccessImpl.hget(addressType, addressCode);
            logger.info("方法--getAllToRedis--省市区编码addressCode:[" + addressCode + "]获取省市区的addressList集合 : {" + addressList + "}");
            return addressList;
        }catch(Exception e){
            return null;
        }
    }
}
